package hw;

import java.util.*;

public class Solution20 {
    static Map<Character, Character> map = new HashMap<>(16);

    static {
        map.put(']', '[');
        map.put('}', '{');
        map.put(')', '(');
    }

    public static boolean isValid(String s) {
        if (s.length() % 2 == 1) {
            return false;
        }
        Deque<Character> charStack = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                if(charStack.isEmpty() || !charStack.peek().equals(map.get(c))){
                    return false;
                }
                charStack.pop();
            } else {
                charStack.push(c);
            }
        }
        return charStack.isEmpty();
    }

    public static void main(String[] args) {
        String s = "([)]";
        System.err.println(isValid(s));
    }
}
